<?php
/**
 *  用户模型类
 */
namespace app\admin\model;
use think\Model;
use think\Db;
class User extends Model
{

    // 获取用户列表
    public function getList()
    {
		$Where = array();
        $result = $this
            ->order('id asc')
            ->select();
        // 获取每用户的第一角色
        foreach ($result as $key => $value) {
            if ($value['ucode'] == 'cb20001') {
                $value['rolename'] = '超级管理员';
                continue;
            }
            $roles = $this->getUserRole($value['ucode'])->toArray();
            if ($roles) {
                $value['rolename'] = $roles['0']['name'];
            } else {
                $value['rolename'] = '';
            }
        }
        return $result;
    }

    // 查找用户资料
    public function findUser($UrlParam)
    {
		$Where = array();
		if(!empty($UrlParam['keyword'])) $Where['username']=['like','%'.trim($UrlParam['keyword']).'%'];
        $result = $this
            ->order('id DESC')
            ->where($Where)
            ->select();		
        // 获取每用户的第一角色
        foreach ($result as $key => $value) {
            if ($value['ucode'] == 'cb20001') {
                $value['rolename'] = '超级管理员';
                continue;
            }
            $roles = $this->getUserRole($value['ucode'])->toArray();
            if ($roles) {
                $value['rolename'] = $roles['0']['name'];
            } else {
                $value['rolename'] = '';
            }
        }
        return $result;
    }

    // 获取用户列表
    public function getSelect()
    {
        return $this->field('ucode,username,realname')
            ->order('id DESC')
            ->select();
    }

    // 检查用户
    public function checkUser($where)
    {
		$whered=array();
		if($where['ucode']) $whered['ucode']=['<>',$where['ucode']];
		if($where['username']) $whered['username']=['eq',$where['ucode']];
        return $this->field('id')
            ->where($whered)
            ->find();
    }

    // 获取用户详情
    public function getUser($ucode)
    {
        $result = $this->where("ucode",$ucode)->find();
        // 用户角色信息
        if ($result) {
            $roles = $this->getUserRole($ucode);
            $result->roles = $roles;
			$result->rcodes = get_mapping($roles, 'rcode');
        }
        return $result;
    }

    // 获取最后一个code
    public function getLastCode()
    {
        return $this->order('id DESC')->value('ucode');
    }

    // 添加用户
    public function addUser(array $data,$roles)
    {
        $result = $this->insert($data);
        if ($result && $roles) {
            $this->addUserRole($data['ucode'], $roles);
        }
        return $result;
    }

    // 删除用户
    public function delUser($ucode)
    {
        $result = $this->where("ucode='$ucode'")->delete();
        if ($result) {
            $this->delUserRole($ucode);
        }
        return $result;
    }

    // 修改用户资料
    public function modUser($ucode, $data, $roles = null)
    {
		
        $result = $this->where("ucode='$ucode'")->update($data);
           
            if (isset($roles)) {
                if (array_key_exists('ucode', $data)) {
                    $ucode = $data['ucode'];
                }
				$result = $this->delUserRole($ucode);
                if ($ucode!= 'cb20001'){					
                    $result = $this->addUserRole($ucode,$roles);
				}
            }
        return $result;
    }

    // 获取指定用户角色表
    private function getUserRole($ucode)
    {
        return Db::name('user_role')
			->alias('u')
			->field('r.rcode,r.name')
			->join('role r', 'u.rcode=r.rcode', 'left')
            ->where("u.ucode='$ucode'")
            ->select();
    }

    // 插入用户角色关联数据
    private function addUserRole($ucode,$roles)
    {
		$data['ucode']=$ucode;
		$data['rcode']=$roles;
        return Db::name('user_role')
            ->insert($data);
    }

    // 删除用户角色关联数据
    private function delUserRole($ucode)
    {
        return Db::name('user_role')->where("ucode","".$ucode."")->delete();
    }
}